package Client;
import javax.swing.*;

import Email.Email;
import Entities.Candidate;
import Entities.Voter;
import Helpers.DBMethods;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

//public class confirmVote implements ActionListener{
//	JFrame Nframe;
//	Boardcv mpanel;
//	Candidate candidate;
//	Voter vo;
//	Boardb boardb;
//	public confirmVote(Candidate candidate, Voter vo, Boardb boardb){
//		this.boardb = boardb;
//		this.candidate = candidate;
//		this.vo = vo;
//		Nframe = new JFrame("Online Voting System!");
//		Nframe.setSize(600,600);
//		Nframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		Nframe.setResizable(true);
//		try {
//			mpanel = new Boardcv(candidate,vo);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		Nframe.setContentPane(mpanel);	
//
//		JButton back = new JButton("Back");
//		back.setBounds(60, 350, 150, 30);
//		back.addActionListener(this);
//		back.setActionCommand("back");
//		mpanel.add(back);
//		back.setVisible(true);
//		Nframe.setVisible(false);
//	}
//
//	public void setV(){
//		Nframe.setVisible(true);
//	}
//
//	public void actionPerformed(ActionEvent e){
//		if(e.getActionCommand().equals("back")){
//			this.setV2();
//			boardb.setVisible(true);
//		}
//	}
//
//	private void setV2() {
//		Nframe.setVisible(false);
//	}
//}
@SuppressWarnings("serial")
class Boardcv extends JPanel implements ActionListener{	
	JLabel voteLabel, titleVote, confirmation,errorCaptcha,errorSQL;
	JButton vote;
	OurCaptcha oc;
	DBMethods dbm = new DBMethods();
	Voter vo;
	Candidate c;
	JFrame mframe;
	JButton back = new JButton("Back");
	public Boardcv(Candidate c,Voter vo,JFrame mframe) {
		this.mframe = mframe;
		back.setBounds(40, 350, 85, 30);
		back.addActionListener(this);
		back.setActionCommand("back");
		add(back);
		this.vo = vo;
		this.c = c;
		setLayout(null); 	
		setBackground(Color.WHITE);	
		oc = new OurCaptcha();
		oc.setBounds(40, 120, 200, 160);
		add(oc);
		
		titleVote = new JLabel("Voting");
		confirmation = new JLabel("You have just voted!");
		errorCaptcha = new JLabel("the captcha is wrong");
		errorSQL = new JLabel("Something wrong with SQL");
		voteLabel = new JLabel("You have chosen "+c.getName()+" - "+c.getSemster()+"th semster to vote for him.");
		vote = new JButton("Vote!");
		
		titleVote.setBounds(200,20,400,30);		// x/y/width/length
		voteLabel.setBounds(40,80,400,30);
		vote.setBounds(300, 350, 100, 30);
		confirmation.setBounds(325,400,200,30);
		errorCaptcha.setBounds(325,400,200,30);
		errorSQL.setBounds(325,400,200,30);
		
		titleVote.setFont(new Font("Dialog", Font.PLAIN, 24));		

		vote.addActionListener(this);
		vote.setActionCommand("vote");	
		add(titleVote);
		add(voteLabel);
		add(vote);
		add(confirmation);
		add(errorCaptcha);
		add(errorSQL);
		
		titleVote.setVisible(true);
		confirmation.setVisible(false);
		errorCaptcha.setVisible(false);
		errorSQL.setVisible(false);
		vote.setVisible(true);
		voteLabel.setVisible(true);		
	}
	
	public void actionPerformed(ActionEvent e){	
		if(e.getActionCommand().equals("vote")){
			if(oc.validateAnswer(oc.getText()))
			{
				VotingClient client = new VotingClient();
				int rsIncoming = 0;
				try {
					rsIncoming = Integer.parseInt(client.sendMessage("insVote#"+vo.getId()+"#"+c.getCandidateID()));
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
//				int rs = dbm.insertVote(vo.getId(), c.getCandidateID());
				
				if(rsIncoming==1)
				{
					new Email().sendVoting(vo.getEmail());
					mframe.getContentPane().removeAll();
					mframe.repaint();
					JPanel p = new Boardb(vo,mframe);
					p.setBounds(0, 0, 600, 600);
					mframe.add(p);//.setV3();
					mframe.validate();
					mframe.repaint();
					System.out.println("go to next view");
				}
				else
				{
					confirmation.setVisible(false);
					errorSQL.setVisible(true);
					errorCaptcha.setVisible(false);
				}
				
			}
			else
			{
				confirmation.setVisible(false);
				errorSQL.setVisible(false);
				errorCaptcha.setVisible(true);
			}
		}
		else if(e.getActionCommand().equals("back")){
			mframe.getContentPane().removeAll();
			mframe.repaint();
			JPanel p = new Boardb(vo,mframe);
			p.setBounds(0, 0, 600, 600);
			mframe.add(p);//.setV3();
			mframe.validate();
			mframe.repaint();
		}
	}
}

